package com.google.code.virtualhockey.vhx.server.model

import com.google.code.virtualhockey.vhx.sds.fields._
import com.google.code.virtualhockey.vhx.sds.{SNameKey, SKey, STMetaData, STNamedEntity}

/**
 * Represents a national league.
 *
 * @param cpCode the league code
 * @param union reference to the NationalUnion the League belongs to
 * @param level the league level (1 = top level league)
 * @param isLastLevel indicates whether this League is the last national level (i.e. no further lower levelled leagues exist)
 * @param model the name of the League's model which determines the competition format
 */
class League( cpCode: String,
              val union: SKey[NationalUnion],
              val level: Long,
              val isLastLevel: Boolean,
              val model: String ) extends STNamedEntity[League] {

  setEntityName( cpCode )

  /** Private no-arg constructor for SDS. */
  private def this() = this( null, null, 0L, false, null )
}

object LeagueMeta extends STMetaData[League] {
  val cls = classOf[League]
  val kind = "League"
  val fields = List(
    new SKeyField( this, "union" ),
    new SLong( this, "level", true ),
    new SBoolean( this, "isLastLevel", false ),
    new SShortText( this, "model", true ) )

  /**
   * Creates a new League instance from a CSV file line.
   */
  override def fromCsv( values: List[Any] ): League = {
    new League( values(0).toString,
                new SNameKey[NationalUnion]( values(1).toString ),
                values(2).asInstanceOf[Long],
                values(3).asInstanceOf[Boolean],
                values(4).toString )
  }
}
